/*
 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SLE_STATE_MACHINE_H
#define SLE_STATE_MACHINE_H

#include <string>
#include <memory>
#include <map>
#include <mutex>
#include "sle_message.h"
#include "sle_device_config.h"
#include "sle_common_config.h"
#include "sle_error_code.h"

namespace OHOS {
namespace NearLink {
namespace SleStandard {
class State {
public:
    explicit State(const std::string &name) : name_(name) {};
    virtual ~State() {};

    virtual bool Entry() = 0;
    virtual void Exit() = 0;
    virtual bool Dispatch(const Message &msg) = 0;

    const std::string &Name() const
    {
        return name_;
    }

private:
    State() = delete;
    std::string name_ {""};
    SLE_DISALLOW_COPY_AND_ASSIGN(State);
};

class StateMachine {
public:
    StateMachine() {};
    virtual ~StateMachine() {};
    bool InitState(const std::string &name);
    bool Move(std::unique_ptr<State> &state);
    bool Transition(const std::string &name);
    bool ProcessMessage(const Message &msg);
    const State *GetLastState() const;
    const State *GetState() const;

private:
    State *last_ = {nullptr};
    State *current_ {nullptr};
    std::map<std::string, std::unique_ptr<State>> states_ {};
    std::mutex stateMtx_;
    SLE_DISALLOW_COPY_AND_ASSIGN(StateMachine);
};
} // SleStandard
} // NearLink
} // OHOS

#endif // SLE_STATE_MACHINE_H